import * as express from "express";
import * as path from "path";
import {User} from '../models/User';
import {BaseController} from './BaseController';
import {AddProjectDto} from '../dtos/projects/AddProjectDto';
import {UpdateProjectDto} from '../dtos/projects/UpdateProjectDto';
import {ProjectsService} from '../services/ProjectsService';
import {Project} from '../models/Project';
export class ProjectController extends BaseController
{
  projectsService:ProjectsService;
  constructor(app:express.Application){
    super(app);
    var $this = this;
    app.post(this.domain+"/projects",(req,res)=>{
      $this.handerErr(req,res,(user:User)=>{
        $this.addProjects(req,res,user);
      },true);
    });
    app.delete(this.domain+"/projects/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.deleteProject(req,res);
      },true);
    });
    app.get(this.domain+"/projects",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getProjects(req,res)
      },true);

    });
    app.get(this.domain+"/projects/user",(req,res)=>{
      $this.handerErr(req,res,(user:User)=>{
        $this.getMyProjects(req,res,user);
      },true);

    });
    app.get(this.domain+"/projects/user/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getProjectsByUserId(req,res);
      });

    });
    app.put(this.domain+"/projects/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.updateProject(req,res);
      },true);

    });
    app.get(this.domain+"/projects/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getProject(req,res);
      },true);

    });
    this.projectsService = new ProjectsService();
  }

  addProjects(req: express.Request, res: express.Response,user:User)
  {
    var userId =  user._id;
    this.projectsService.add(AddProjectDto.toProject(userId,req.body),(err,project:Project)=>{
      res.send(JSON.stringify(project));
    });
  }

  getProject(req: express.Request, res: express.Response)
  {
    super.initResponse(res);
    var id = req.params.id;
    this.projectsService.getProject(id,(err,project)=>{
      res.send(JSON.stringify(project));
    });
  }

  getProjects(req: express.Request, res: express.Response)
  {
    super.initResponse(res);
    this.projectsService.getProjects((err,projects)=>{
      res.send(JSON.stringify(projects));
    });
  }

  getProjectsByUserId(req: express.Request, res: express.Response)
  {
    super.initResponse(res);
    var userId = req.params.id;
    this.projectsService.getMyProjects(userId,(err,projects)=>{
      res.send(JSON.stringify(projects));
    });
  }

  getMyProjects(req: express.Request, res: express.Response,user:User)
  {
    var userId = user._id;
    this.projectsService.getMyProjects(userId,(err,projects)=>{
      res.send(JSON.stringify(projects));
    });
  }

  updateProject(req: express.Request, res: express.Response)
  {
    var id = req.params.id;
    var project:Project = UpdateProjectDto.toProject(id,req.body);
    this.projectsService.updateProject(project,(err,project)=>{
      res.send(JSON.stringify(project));
    });
  }

  deleteProject(req: express.Request, res: express.Response)
  {
    var id = req.params.id;
    this.projectsService.deleteProject(id,(err,project)=>{
      res.send(JSON.stringify(project));
    });
  }

}
